package com.link.base.base.contract.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.link.base.base.contract.model.ContractPayInfo;
import com.link.base.base.contract.service.ContractPayInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;

import java.math.BigDecimal;

/**
 * 支付情况
 *
 * @date 2023-06-26 07:37:27
 */
@Controller
@RequestMapping("/link/contractPayInfo")
public class ContractPayInfoController extends BasicController<ContractPayInfo> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ContractPayInfoService contractPayInfoService;

    @Override
    public BasicService<ContractPayInfo> getBasicService() throws Exception {
        return contractPayInfoService;
    }

    public void beforUpsert(ContractPayInfo entity, HttpServletRequest request) throws Exception {

        if (null == entity.getContractId()) {
            throw new RuntimeException("请先保存合同!");
        }
        // 支付金额
        Double payAmount = entity.getPayAmount();
        // 保留两位小数
        BigDecimal payAmountBg = new BigDecimal(payAmount);
        double payAmountDb = payAmountBg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        entity.setPayAmount(payAmountDb);

        // 支付比例
        Double payRatio = entity.getPayRatio();
        if (payRatio < 0 || payRatio > 100) {
            throw new RuntimeException("支付比例（%）必须在[0,100]!");
        }


    }
}
